package org.molamil.acto.commands {
	import org.molamil.acto.utils.ObjectUtils;

	import flash.net.URLVariables;
	import flash.errors.IllegalOperationError;
	import flash.net.URLRequest;
	import flash.utils.getQualifiedClassName;
	import flash.net.URLRequestMethod;

	/**
	 * @author Jorge Hernandez
	 */
	public class AbstractRemoteCommand extends AbstractValidatorCommand {
		
		
		// PROPERTIES
		
		public var method:String;
		public var contentType:String;
		
		//TODO: Implement request headers.
		
		protected var urlRequest:URLRequest;
		protected var data:*;
		
		
		// CONSTRUCTOR
		
		public function AbstractRemoteCommand() {
			
			// Abstract check.
			if (getQualifiedClassName(this) == "org.molamil.acto.commands::AbstractRemoteCommand")
            	throw new IllegalOperationError("AbstractRemoteCommand must be extended");
            	
			method = URLRequestMethod.POST;
			
		}
		
		
		// PROTECTED METHODS
		
		protected function prepareUrlRequest():void {
			
			urlRequest = new URLRequest(context.target);
			urlRequest.method = method;
			urlRequest.data = new URLVariables();
			
			if (contentType != null)
				urlRequest.contentType = contentType;
			
			if (context.cache.toLowerCase() == CommandContext.CACHE_CLEAR.toLowerCase()) {
				var stamp:String = "" + stamp;
				urlRequest.data[stamp] = stamp;
			}
			
			ObjectUtils.mergeDynamic(urlRequest.data, decorateParams(request.params));
			
		}
		
		protected function decorateParams(params:Object):Object {
        	return params;
		}
		
		
	}
	
}
